package hotel.quartz.oversea.job.task;

import com.alibaba.fastjson.JSON;
import hotel.base.oversea.constant.CommonConstants;
import hotel.base.oversea.constant.CommonUrlConstants;
import hotel.base.oversea.constant.enums.MtHotelEnums;
import hotel.base.oversea.constant.quartz.UrlMtEnums;
import hotel.base.oversea.utils.Result;
import hotel.quartz.oversea.vo.meituan.MtHotelOrderJobQueryOrderVo;
import hotel.quartz.oversea.util.JobRestTemplateUtils;
import hotel.quartz.oversea.vo.meituan.MtHotelOrderQueryDetailVo;
import hotel.quartz.oversea.vo.meituan.MtHotelOrderStateHistory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Component
public class MeituanHotelOrderQueryCheckOneOrderTask {
    @Autowired
    private RestTemplate restTemplate;
    private String timestamp;
    void setTimestamp(String timestamp){
        this.timestamp = timestamp;
    }
    //@Async 标识这个方法异步执行，可返回void或Future
    /**
     * 校验查询出来的订单状态，更新订单状态相关信息
     */
    @Async
    public boolean checkOneOrder(MtHotelOrderQueryDetailVo thisOrderResonse, Integer orderStatus, String mtOrderId, String distributorOrderId, String timestamp) {
        setTimestamp(timestamp);
        log.error("Meituan Hotel Query Job MeituanHotelOrderQueryTask {} 酒店主动查询订单定时器进入方法 requestSaveWaitDataApi 查询出订单实时状态，进入订单校验和更新 checkOneOrder: {},{},{}",timestamp,orderStatus,mtOrderId,distributorOrderId);

        String host = CommonUrlConstants.MEITUAN_HOST;
        String updateUrl = host + UrlMtEnums.hotelOrderQuery.ORDER_CHECK_UPDATE.getValue();//+"?access_token="+accessToken;

        MtHotelOrderJobQueryOrderVo mtHotelOrderJobQueryOrderVo = new MtHotelOrderJobQueryOrderVo();
        //mtHotelOrderJobQueryOrderVo.setDetail(thisOrderResonse);
        mtHotelOrderJobQueryOrderVo.setOrderStatus(orderStatus);
        mtHotelOrderJobQueryOrderVo.setMtOrderId(mtOrderId);
        mtHotelOrderJobQueryOrderVo.setDistributorOrderId(distributorOrderId);

        //构造日志对象
        MtHotelOrderStateHistory stateHistory = new MtHotelOrderStateHistory();
        stateHistory.setMtDistributorOrderId(distributorOrderId);
        stateHistory.setMtOrderId(mtOrderId);
        stateHistory.setStatusCode(orderStatus);
        stateHistory.setStatusDesc(null);
        stateHistory.setMtHotelId(null);
        stateHistory.setMtGoodsId(null);
        stateHistory.setMethod(null);
        stateHistory.setMethodName(MtHotelEnums.apiMethodEnum.METHOD_HOTEL_ORDER_QUERY.getName());
        stateHistory.setClassJson(JSON.toJSONString(thisOrderResonse));
        stateHistory.setMtOrderState(orderStatus);

        Map params = new HashMap();
        params.put("source"," 主动查询美团定时器 ");
        params.put("mtHotelOrderJobQueryOrderVo", JSON.toJSONString(mtHotelOrderJobQueryOrderVo));
        params.put("stateHistory", JSON.toJSONString(stateHistory));
        //发起请求
        Result response = JobRestTemplateUtils.post(updateUrl, JSON.toJSONString(params),restTemplate);
        if(response.getCode() == CommonConstants.SUCCESS){
            log.info("MeituanHotelOrderQueryCheckOneOrderTask {} 校验查询出来的订单状态，更新订单状态相关信息  checkOneOrder 请求成功：{}，{}",timestamp,response, JSON.toJSONString(params));
            return true;
        }else{
            log.info("MeituanHotelOrderQueryCheckOneOrderTask {} 校验查询出来的订单状态，更新订单状态相关信息  checkOneOrder 请求失败：{}，{}",timestamp,response, JSON.toJSONString(params));
        }
        return false;
    }
}
